﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Text.RegularExpressions;

namespace Utils
{
    public static class StringHelper
    {
        public static string ToEmailHtml(this string str)
        {
            if (!string.IsNullOrEmpty(str))
            {
                Regex regex = new Regex(@"(\r\n|\r|\n)");

                return string.IsNullOrEmpty(str) ? "" : regex.Replace(str, "<br />");
            }
            else
            {
                return string.Empty;
            }
        }

        public static string ToPreviewHtml(this string str)
        {
            if (!string.IsNullOrEmpty(str))
            {
                str = str.Replace("<", "&lt;");
                str = str.Replace(">", "&gt;");
                Regex regex = new Regex(@"(\r\n|\r|\n)");

                return string.IsNullOrEmpty(str) ? "" : regex.Replace(str, "<br />");
            }
            else
            {
                return string.Empty;
            }
        }

        public static string Replace(this string str, string oldValue, string newValue, StringComparison comparison)
        {
            StringBuilder sb = new StringBuilder();

            int previousIndex = 0;
            int index = str.IndexOf(oldValue, comparison);
            while (index != -1)
            {
                sb.Append(str.Substring(previousIndex, index - previousIndex));
                sb.Append(newValue);
                index += oldValue.Length;

                previousIndex = index;
                index = str.IndexOf(oldValue, index, comparison);
            }
            sb.Append(str.Substring(previousIndex));

            return sb.ToString();
        }

        public static string ToDomainName(this string url)
        {
            if (string.IsNullOrEmpty(url))
            {
                return string.Empty;
            }
            else
            {
                string key1 = "://";
                string key2 = "/";
                int index = url.IndexOf(key1);
                if (index >= 0)
                {
                    string domain = url.Substring(index + key1.Length);
                    int index2 = domain.IndexOf(key2);
                    if (index2 >= 0)
                    {
                        domain = domain.Substring(0, index2);
                    }
                    return domain;
                }
                else
                {
                    return url;
                }
            }

        }

        public static string ToRichHtml(this string html)
        {
            if (string.IsNullOrEmpty(html))
            {
                return string.Empty;
            }
            else
            {
                return html.Replace("<p></p>", "<p><br></p>");
            }

        }

        public static string ToUpperAllFirstLetter(this string value)
        {
            if (string.IsNullOrEmpty(value)) return string.Empty;

            char[] array = value.ToCharArray();
            // Handle the first letter in the string.
            if (array.Length >= 1)
            {
                if (char.IsLower(array[0]))
                {
                    array[0] = char.ToUpper(array[0]);
                }
            }
            // Scan through the letters, checking for spaces.
            // ... Uppercase the lowercase letters following spaces.
            for (int i = 1; i < array.Length; i++)
            {
                if (array[i - 1] == ' ')
                {
                    if (char.IsLower(array[i]))
                    {
                        array[i] = char.ToUpper(array[i]);
                    }
                }
            }
            return new string(array);
        }
    }
}